import { InteractiveModel } from '@/api/manager/interactive/interface'
import { compareMtKey } from '../../../../utils/utils'

export const mtListInteractiveSort = (mtList: InteractiveModel[], sortType: number, vipMtKeys: string[]) => {
	let result = [] as InteractiveModel[]
	//mtList里的 vip 和 vip列表的vip 不一定一致，以mtList里的为主，vip列表为辅
	const vipInMtList = mtList.filter((x) => x.mcc.status.isVip)
	let _arr = Array(vipMtKeys.length) as InteractiveModel[]
	for (const vip of vipInMtList) {
		const index = vipMtKeys.findIndex((mtKey) => mtKey === vip.mtKey)
		if (index !== -1) {
			_arr[index] = vip
		} else {
			_arr.push(vip)
		}
	}
	result = _arr.filter(Boolean)
	const exceptVipInMtList = mtList.filter((x) => !x.mcc.status.isVip)
	if (sortType === 1) {
		result = [...result, ...exceptVipInMtList.sort((a, b) => a.mcc.index - b.mcc.index)]
	} else if (sortType === 2) {
		result = [
			...result,
			...exceptVipInMtList.sort(
				(a, b) =>
					b.attr.weightExceptVipInSortType2 - a.attr.weightExceptVipInSortType2 ||
					compareMtKey(a.mtKey, b.mtKey)
			),
		]
	} else if (sortType === 3) {
		result = mtList
	}
	return result
}
